package com.wanmait.deliverymangementsystem.runnercontroller;

import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.pojo.Orders;
import com.wanmait.deliverymangementsystem.pojo.Runner;
import com.wanmait.deliverymangementsystem.pojo.Tasks;
import com.wanmait.deliverymangementsystem.service.OrdersService;
import com.wanmait.deliverymangementsystem.service.RunnerService;
import com.wanmait.deliverymangementsystem.service.TaskService;
import com.wanmait.deliverymangementsystem.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("runner")
public class RunnerOrdersController {

    @Resource
    private OrdersService ordersService;
    @Resource
    private UsersService usersService;
    @Resource
    private TaskService taskService;
    @Resource
    private RunnerService runnerService;

    //获取 可接收任务
    @GetMapping("availableTaskList")
    @ModelAttribute("ordersPageInfo")
    public PageInfo<Orders> availableTaskList(HttpSession session,Integer page){
        Runner runner =(Runner) session.getAttribute("runner");
        if (runner.getAvailable()) {
            if (page==null)
                page=0;
            PageInfo<Orders> ordersPageInfo = ordersService.runnerFindWaitOrder(page, runner.getCity().getId());
            return ordersPageInfo;
        }
        return null;
    }
    //接收任务页面
    @GetMapping("accept")
    @ModelAttribute("orders")
    public Orders accept (Integer id){
    Orders orders = ordersService.findOrder(id);
    orders.setTask(taskService.findByID(orders.getTask().getId()));
    orders.setUsers(usersService.findUser(orders.getUsers().getId()));
    return  orders;
    }

    //接收
    @RequestMapping("apt")
    public String apt(Integer id, Integer minus, HttpSession session){
        Tasks tasks =ordersService.findOrder(id).getTask();
        tasks=taskService.findByID(tasks.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date =new Date();
        date.setMinutes(date.getMinutes()+minus);
        try {
            date=  sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Runner runner =(Runner) session.getAttribute("runner");
        tasks.setRunner(runner);
     java.sql.Timestamp date1 =new java.sql.Timestamp(date.getTime());
        tasks.setEstimatedTime(date1);
        taskService.accept(tasks);
        Orders orders =ordersService.findOrder(id);
        orders.setRunner(runner);
        ordersService.accept(orders);
        if (ordersService.findRunnerTaskingNumber(runner.getId())==5){
            runnerService.taskAvailable(false,runner.getId());
            runner.setAvailable(false);
            session.setAttribute("runner",runner);
        }
        return "redirect:/runner/taskingList";
    }

        //配送详情
    @GetMapping("delivering")
    @ModelAttribute("order")
    public Orders delivering(Integer id){
        Orders order=ordersService.findBeforeDelivery(id);
        return order;
    }


        //完成任务
    @GetMapping("complete")
    public String complete(Integer id, @SessionAttribute("runner")Runner runner, HttpSession session){
        ordersService.updateStatus(id,3);
        Orders orders =ordersService.findOrder(id);
        Tasks tasks=taskService.findByID(orders.getTask().getId());
        runnerService.taskAvailable(true,runner.getId());
        double money=runner.getWallet()+tasks.getReward().doubleValue()*0.7;
        DecimalFormat df =new DecimalFormat("#.00");
        money=Double.parseDouble(df.format(money));
        runner.setWallet(money);
        runnerService.updateWallet(runner);
        runner.setAvailable(true);
        if (runner.getCompletedOrderNumber()==null){
            runner.setCompletedOrderNumber(1);
        }
        else
            runner.setCompletedOrderNumber(runner.getCompletedOrderNumber()+1);
        runnerService.addOrderNumber(runner);
        session.setAttribute("runner",runner);



        return "redirect:/runner/taskingList";
    }
        //正在进行的订单
    @GetMapping("taskingList")
    @ModelAttribute("ordersPageInfo")
    public PageInfo<Orders>taskingList(HttpSession session,Integer page){
        Runner runner = (Runner) session.getAttribute("runner");
        if (page==null)
            page=0;
        PageInfo<Orders> ordersPageInfo =ordersService.runnerFindOrdering(page,runner.getId());

        return ordersPageInfo;
    }
        //订单详情
    @GetMapping("detail")
    public String detail(Integer id, Integer status){
        if (status==1){
            return "redirect:/runner/aming?id="+id;
        }
        else {
            return "redirect:/runner/delivering?id="+id;
        }
    }

    // 取物品详情
    @GetMapping("aming")
    @ModelAttribute("order")
    public Orders aming(Integer id){
        Orders order =ordersService.findBeforeDelivery(id);
        return order;
    }
    //去配送
    @GetMapping("goDelivery")
    public String goDelivery(Integer id){
        ordersService.updateStatus(id,2);
        return "redirect:/runner/taskingList";
    }
    @GetMapping("completedOrderList")
    @ModelAttribute("ordersPageInfo")
    public PageInfo<Orders> completedOrderList(Integer page,@SessionAttribute Runner runner){
        if (page==null)
            page=0;
       PageInfo<Orders> ordersPageInfo= ordersService.findCompletedByRunnerID(runner.getId(),page);
       return ordersPageInfo;
    }
    @GetMapping("delteComOrder")
    public String delteComOrder(Integer id){
        ordersService.deleteRunner(id);
        return "redirect:/runner/completedOrderList";
    }

}
